package com.jiaquyun.jcps.ui.access

import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import com.jakewharton.rxbinding2.widget.RxTextView
import com.jiaquyun.jcps.R
import com.jiaquyun.jcps.base.BaseAbstractFragment
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Function4
import kotlinx.android.synthetic.main.fragment_access_register.*

class RegisterFragment : BaseAbstractFragment() {
    override val layoutResId: Int
        get() = R.layout.fragment_access_register


    companion object {
        fun newInstance(): RegisterFragment {
            return RegisterFragment()
        }
    }

    override fun initView() {
        observableRetrieve()

        //显示密码
        cbRegisterEye.setOnCheckedChangeListener { _, isChecked ->
            etRegisterPwd.transformationMethod = if (isChecked)
                HideReturnsTransformationMethod.getInstance()
            else PasswordTransformationMethod.getInstance()
        }
    }


    /**
     * 注册输入框监听
     * */
    private fun observableRetrieve() {
        val observablePhone: Observable<CharSequence> =
            RxTextView.textChanges(etRegisterPhone)
        val observableCode: Observable<CharSequence> =
            RxTextView.textChanges(etRegisterCode)
        val observableAccount: Observable<CharSequence> =
            RxTextView.textChanges(etRegisterAccount)
        val observablePwd: Observable<CharSequence> =
            RxTextView.textChanges(etRegisterPwd)
        val observable: Disposable =
            Observable.combineLatest(observablePhone,
                observableCode,
                observableAccount,
                observablePwd,
                Function4<CharSequence, CharSequence, CharSequence, CharSequence, Boolean> { t1, t2, t3, t4 ->
                    t1.isNotEmpty() && t2.isNotEmpty() && t3.isNotEmpty() && t4.isNotEmpty()
                })
                .subscribe {
                    tvRegisterSubmit.isEnabled = it
                }
        dispose.add(observable)
    }
}